package junit.admin;

import com.jfinal.aop.Inject;
import io.jboot.test.MockMvcResult;
import io.jboot.utils.StrUtil;
import io.jpress.JPressOptions;
import io.jpress.module.article.service.ArticleService;
import junit.base.AdminBaseTester;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class AdminOptionsTester extends AdminBaseTester {

    @Inject
    private ArticleService articleService;

    @Test
    public void startOptionsTestCases() {

        //用户登录，默认账号 admin 123456
        doLogin();

        //保存配置信息
        testDoSaveOption();

        //读取配置信息
        doReadOption();

        //删除配置信息
        testDeleteOption();

    }


    /**
     * 保存配置信息
     */
    public void testDoSaveOption() {

        Map<String, Object> paras = new HashMap<>();
        paras.put("key001", "value001");


        MockMvcResult mvcResult = postWithCsrfToken("/admin/option/doSave", paras);
        Assert.assertEquals(mvcResult.getStatus(), 200);

        mvcResult.printResult();
    }


    /**
     * 读取配置信息
     */
    public void doReadOption() {
        final String value = JPressOptions.get("key001");
        Assert.assertEquals(value, "value001");
    }


    /**
     * 删除配置信息
     */
    public void testDeleteOption() {
        Map<String, Object> paras = new HashMap<>();
        paras.put("key001", "");


        MockMvcResult mvcResult = postWithCsrfToken("/admin/option/doSave", paras);
        Assert.assertEquals(mvcResult.getStatus(), 200);

        mvcResult.printResult();

        final String value = JPressOptions.get("key001");
        Assert.assertTrue(StrUtil.isNullOrBlank(value));
    }


}
